package com.farm.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.farm.entity.Order;
import com.farm.entity.OrderStatus;
import com.farm.entity.User;

public interface OrderRepository extends JpaRepository<Order, Long> {
    List<Order> findByUser(User user);
    List<Order> findByStatus(OrderStatus status);
    List<Order> findByUserAndStatus(User user, OrderStatus status);
    
    @Query("SELECT DISTINCT o FROM Order o JOIN o.items i WHERE i.product.farmer = :farmer")
    List<Order> findByFarmer(@Param("farmer") User farmer);

    @Query("SELECT o FROM Order o JOIN o.user u WHERE u.realName LIKE CONCAT('%', :keyword, '%')")
    List<Order> findByUserNameContaining(@Param("keyword") String keyword);

    @Query("SELECT o FROM Order o JOIN o.user u WHERE u.realName LIKE CONCAT('%', :keyword, '%') AND o.status = :status")
    List<Order> findByUserNameContainingAndStatus(@Param("keyword") String keyword, @Param("status") OrderStatus status);
} 